
# A2b_plot_export_prices_EU
#===============================================================================

# Description: This file plots average export prices for EU countries and creates 
# one of the Figure 1 subplots

rm(list = ls())

setwd("D:/data_replication")

library("data.table")  
library("ggplot2")  
library("ggrepel")  
library("stargazer")  

output <- fread('statistics_and_graphs/export_prices/output_files/prices_exporter.txt')
output[ , V1 := gsub("s", "", V1)]
output <- output[V1 != ""]
output <- output[V1 != "_con"]
setnames(output, "V1", "partner")
output[ , partner := as.numeric(partner)]
setnames(output, "V2", "price")
output[ , price := as.numeric(price)]

gdp_data <- fread('data/gdp_per_capita/gdp_per_capita_defl.csv')
gdp_data <- gdp_data[year == 2007]

output <- merge(output, gdp_data, by = "partner")
output <- as.data.table(output)

iso3 <- fread('data/country_codes/ISO3_partner_codes.csv') 
output <- merge(output, iso3, by = "partner")
output <- as.data.table(output)

output[ , gdp_log := log(gdp_per_capita_defl)]


output <- output[partner == 3 | partner == 4 | partner == 5 | partner == 6 |
                 partner == 7 | partner == 8 | partner == 9 | partner == 10 |
                 partner == 11 | partner == 17 | partner == 18 | partner == 30 |  
                 partner == 32 | partner == 38 | partner == 46 | partner == 53 |
                 partner == 54 | partner == 55 | partner == 60 | partner == 61 |
                 partner == 63 | partner == 64 | partner == 66 | partner == 68 |
                 partner == 91 | partner == 600]


plot_export_prices <- ggplot(output, aes(x=gdp_log, y=price)) +
                geom_text_repel(label=output$partner_description, size = 6) +
                theme(text = element_text(size=17)) +
                geom_smooth(method=lm) +
                labs(x = "Exporter GDP per capita (in logs)", y = "Avg. Export Price") +
                theme(axis.title=element_text(size=30)) +
                theme(axis.text=element_text(size=18))
                
ggsave("statistics_and_graphs/export_prices/plot_export_prices_EU.png", plot = plot_export_prices, width = 12, height = 9)

